package com.lry.web.config;

import com.lry.web.rests.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

@Configurable
@SpringBootConfiguration
public class ShiroConfig {
    /**
     *  创建ShiroFilterFactoryBean
     */
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager ){
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    //设置安全管理器
    shiroFilterFactoryBean.setSecurityManager(securityManager);

    //未登录跳转页面
    shiroFilterFactoryBean.setLoginUrl("/index");
    //未授权跳转页面
    shiroFilterFactoryBean.setUnauthorizedUrl("/index");

    Map<String,String> filterMap = new HashMap<String,String>();
    //必须认证才可以访问
    filterMap.put("/","anon");
    filterMap.put("/*/search/**","anon");
    filterMap.put("/static/**","anon");
    filterMap.put("/user/getValidateCode","anon");
    filterMap.put("/user/login/**","anon");
    filterMap.put("/index","anon");
    filterMap.put("/admin/login","anon");
    filterMap.put("/**","authc");
    shiroFilterFactoryBean  .setFilterChainDefinitionMap(filterMap);
    return shiroFilterFactoryBean;
}


    /**
     *  创建DefaultWebSecurityManager
     */
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }
    /**
     *  创建Realm
     */
//    @Bean
//    public UserRealm userRealm(){
//        UserRealm userRealm = new UserRealm();
//        return userRealm;
//    }

}